package com.haredot.event.listener;

import com.haredot.constraint.RabbitConst;
import com.haredot.entity.Notify;
import com.haredot.event.RssNotifyEvent;
import com.haredot.event.enums.NotifyType;
import com.haredot.mapper.NotifyMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 通知事件监听器
 */
@Component
public class RssNotifyEventListener implements ApplicationListener<RssNotifyEvent> {

    @Resource
    private NotifyMapper notifyMapper ;

    @Resource
    private RabbitTemplate rabbitTemplate ;

    @Override
    public void onApplicationEvent(RssNotifyEvent event) {
        // 获取 通知消息
        Notify notify = event.getNotify() ;
        // 除 公告、私信外、其它均写入到 通知表中
        if (!NotifyType.OFFICIAL.getTypeName().equals(notify.getTypeName()) &&  !NotifyType.LETTER.getTypeName().equals(notify.getTypeName())) {
            notifyMapper.insert(notify) ;
        }
        // 发送消息到 RabbitMQ 通知队列中
        rabbitTemplate.convertAndSend("", RabbitConst.NOTIFY_QUEUE, notify);
    }
}
